home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11611 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: solon.com!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: const pointer confusion...
  5. Date: 25 Mar 1996 06:23:37 -0600
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4j6389$3iq@solutions.solon.com>
  10. References: <4j06gm$7oa@solutions.solon.com> <4j41io$nma@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4j41io$nma@solutions.solon.com>,
  14. Huayong Yang <yang@math.umass.edu> wrote:
  15. >
  16. >const int *p and int const *p are the same: a pointer to const integer;
  17. >int * const p means a const pointer to integer. A little program to
  18. >verify it:
  19.  
  20. This is true. The grammar of the C language allows type specifiers, type
  21. qualifers and storage class specifiers to appear in any order in the syntactic
  22. unit ``declaration-specifiers''. Thus,
  23.  
  24.     const int i;
  25.  
  26. and
  27.     int const i;
  28.  
  29. are essentially the same declaration. It's probably good style to not use any
  30. old arbitrary order, but stick to putting the storage class first (if any),
  31. followed by the const or volatile qualifier, followed by the type.
  32.  
  33. In the case of declarations, there is no ambiguity, since in a declarator only
  34. the '*' keyword can introduce a type qualifier list, and the '*' keyword cannot
  35. appear in a declaration specifier list.
  36.  
  37. So in ``int const i'', the const must belong with the ``int'', since ``const
  38. i'' is not a valid declarator. On the other hand, ``int * const i'' makes the
  39. const belong with the *, since the ``direct-declarator'' i is preceded by an
  40. optional ``pointer'', a syntactic unit which can generate a '*' followed by an
  41. optional list of type qualifiers followed an optional recursive instance of
  42. ``pointer''.
  43. -- 
  44.